#!/usr/bin/ENV perl

use utf8;
use LWP::UserAgent;
use Encode;

binmode STDIN, ":encoding(utf8)";
binmode STDOUT, ":encoding(utf8)";

my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1");

print "Please input the search key\n";
chomp(my $key = <STDIN>);

my $search_url = 'http://www.google.com/search?q=' . $key;

#print $search_url, "\n";

my $req = HTTP::Request->new(GET => $search_url);
$req->content_type('application/x-www-form-urlencoded; charset=utf-8');
#$req->content('query=libwww-perl&mod=dist');

my $res = $ua->request($req);

if ($res->is_success) {
	#my $encoded_content = decode('gbk', $res->content);
	#print $encoded_content;
	#print decode('big5', $res->content);
	my $content = $res->content;

	#foreach ($content =~ /<cite>([\S\s]*?)<\/cite>/ig) {
	#	s#(<b>|</b>)##gi;
	#	print $_, "\n";
	#}
	foreach ($content =~ /<h3 class="r"><a[\S\s]*?>([\S\s]*?)<\/a>/ig) {
		s#(<b>|</b>)##gi;
		print encode('utf8',decode('gbk',$_)), "\n";
	}

} else {
	print $res->status_line, "\n";
}